package controller;

import dao.ArauditorDao;
import dao.ArauditorDetailsDao;
import dao.MarketDao;
import dao.userDao;
import entity.Arauditor;
import entity.ArauditorDetails;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.Impl.MarketArauditorImpl;
import service.MarketArauditorDao;
import service.RetreatDao;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/xiaoshou")
@Scope("prototype")
public class Market {
    @Autowired
    public MarketDao marketDao;//销售临时表dao层
    @Autowired
    public userDao userDao;//用户dao层
    @Autowired
    public ArauditorDao arauditorDao;//销售表dao层
    @Autowired
    public ArauditorDetailsDao arauditorDetailsDao;//销售详情表dao层
    @Resource
    public MarketArauditorDao marketArauditorDao; //service层
    @Autowired
    public RetreatDao retreatDao;//service层

    @RequestMapping("/showmarket")//展示用户所购买的商品
    @ResponseBody
    public Map<String,Object> showMarket( int offset, int limit){
        Map<String,Object> map=new HashMap<>();
        map.put("rows",marketDao.showMarket(offset,limit));
        map.put("total",marketDao.total());
        return map;
    }
    @RequestMapping("/addmarket")//添加用户购买的商品
    @ResponseBody
    public Map<String,Object> addMarket(entity.Market market){
        entity.Market mk=marketDao.repertoryIdMarket(market.getRepertoryId());//判断用户现在这件商品是否结账没结账再次购买数量加总金额加
        if(mk!=null){
            int marketNum=market.getMarketNum()+mk.getMarketNum();
            double totalPrice=market.getTotalPrice()+mk.getTotalPrice();
            marketDao.addMarketNum(marketNum,mk.getMarketid(),totalPrice);
        }else{//如果用户这件商品还没选择哪进行添加这件商品
            marketDao.addMarket(market);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("rows",marketDao.showMarket(0,4));
        map.put("total",marketDao.total());
        return map;//返回用户购买商品菜单上
    }
    @RequestMapping("/delmarket")//删除用户购买的商品
    @ResponseBody
    public Map<String,Object> delMarket(String marketId){
        marketDao.delMarket(Integer.parseInt(marketId));
        Map<String,Object> map=new HashMap<>();
        map.put("rows",marketDao.showMarket(0,4));
        map.put("total",marketDao.total());
        return map;
}
    @RequestMapping("/delmarkets")//删除用户购买的商品
    //删除购买临时表中的所有信息
    public String delmarkets(){
        marketDao.delmarkets();
        System.out.println("删除成功");
        return "/xiaoshou/repertoryxiaoshou.jsp";
    }
    //销售完成添加销售记录和经济记录
    @RequestMapping("/success")
    @ResponseBody
    public  Map<String,Object> success(String userName, String iphoneNum, int radio, double totals, int nums, double price, int zf, HttpServletRequest request){
        marketArauditorDao.success(userName,iphoneNum,radio,totals,nums,price,zf,request);
        marketDao.delmarkets();
        Map<String,Object> map=new HashMap<>();
        map.put("rows",marketDao.showMarket(0,4));
        map.put("total",marketDao.total());
        return map;
    }
    //展示销售审核
    @RequestMapping("/arauditor")
    @ResponseBody
    public Map<String,Object> showArauditor(String seat,String username,String lasttime,String times,int offset, int limit){
        if(username!=null){
            try {
                username = new String(username.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("rows",arauditorDao.showArauditor(seat,username,lasttime,times,offset,limit));
        map.put("total",arauditorDao.total(seat,username,lasttime,times));
        return map;
    }
    //展示销售审核详情
        @RequestMapping("/arauditordetails")
        @ResponseBody
        public List<ArauditorDetails> showArauditorDetails(String seats,String seat,String username,String lasttime,String times,String offset, String limit) {
            if (username != null) {
                try {
                    username = new String(username.getBytes("ISO-8859-1"), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (seats != null) {
                return arauditorDetailsDao.showArauditorDetails(seats);
            } else {
                List<Arauditor> list = arauditorDao.showArauditor(seat, username, lasttime, times, Integer.parseInt(offset),Integer.parseInt(limit));
                if(list.size()==0){
                    List<Arauditor> lists = arauditorDao.showArauditor(seat, username, lasttime, times, Integer.parseInt(offset),Integer.parseInt(limit));
                    Arauditor arauditor = lists.get(0);
                    return arauditorDetailsDao.showArauditorDetails(arauditor.getSeat());
                }else{
                    Arauditor arauditor = list.get(0);
                    return arauditorDetailsDao.showArauditorDetails(arauditor.getSeat());
                }

            }
    }
    //修改审核状态
    @RequestMapping("/arauditorState")
    @ResponseBody
    public Map<String,Object> upArauditorState(String seats,String seat,String username,String lasttime,String times,String offset, String limit,HttpServletRequest request){
        if (username != null) {
            try {
                username = new String(username.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
          marketArauditorDao .shenhe(seats,request);//进行审核和往经济表中录入数据

        Map<String,Object> map=new HashMap<>();
        //根据查询信息分页是否有值
        List<Arauditor> list = arauditorDao.showArauditor(seat, username, lasttime, times, Integer.parseInt(offset),Integer.parseInt(limit));
        if(list.size()==0){//如果没有值的话前进一页
            map.put("rows",arauditorDao.showArauditor(seat,username,lasttime,times, Integer.parseInt(offset)-6,(Integer.parseInt(limit)-1)));
            map.put("total",arauditorDao.total(seat,username,lasttime,times));
        }else{//如果当前页有值的话返回当前页
            System.out.println("55");
            map.put("rows",arauditorDao.showArauditor(seat,username,lasttime,times, Integer.parseInt(offset),Integer.parseInt(limit)));
            map.put("total",arauditorDao.total(seat,username,lasttime,times));
        }
        return map;
    }
    @RequestMapping("/showdanjuucha")//查询已审核的单据
    @ResponseBody
    public Map<String,Object>showdanjucha(String seat,String username,String lasttime,String times,int offset, int limit){
        Map<String,Object> map=new HashMap<>();
        if(username!=null){
            try {
                username = new String(username.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        map.put("rows",arauditorDao.showstatetwo(seat,username,lasttime,times,offset,limit));
        map.put("total",arauditorDao.twototal(seat,username,lasttime,times));
        return map;
    }
    //展示已审核销售审核详情
    @RequestMapping("/twoarauditordetails")
    @ResponseBody
    public List<ArauditorDetails> showArauditorDetailstwo(String seats,String seat,String username,String lasttime,String times,String offset, String limit) {
        if (username != null) {
            try {
                username = new String(username.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if (seats != null) {
            return arauditorDetailsDao.showArauditorDetails(seats);
        } else {
            List<Arauditor> list = arauditorDao.showstatetwo(seat, username, lasttime, times, Integer.parseInt(offset),Integer.parseInt(limit));
                Arauditor arauditor = list.get(0);
                return arauditorDetailsDao.showArauditorDetails(arauditor.getSeat());
        }
    }
    //根据单号展示销售审核信息进行退货
    @RequestMapping("/treeShowArauditorDetails")
    @ResponseBody
    public Map<String,Object> treeShowArauditorDetails(String seat,int offset,int limit){
        arauditorDetailsDao.upout();//把备注和退货数量设置为空
        Map<String,Object> map=new HashMap<>();
        map.put("rows",arauditorDetailsDao.showDetails(seat,offset,limit));
        map.put("total",arauditorDetailsDao.showDetailsTotal(seat));
        return map;
    }
    @RequestMapping("/addTh")
    @ResponseBody
    //增加退货详情
    public Map<String,Object> addTh(String seat,int offset,int limit,int repertoryId,int nums,String beizhu){
        Map<String,Object> map=new HashMap<>();
        arauditorDetailsDao.upTh(seat,repertoryId,beizhu,nums);
        map.put("rows",arauditorDetailsDao.showDetails(seat,offset,limit));
        map.put("total",arauditorDetailsDao.showDetailsTotal(seat));
        return map;
    }
    //往退货表中添加信息
    @RequestMapping("/retreat")
    @ResponseBody
    public Map<String,Object> retreat(String id,String seat,int offset,int limit,HttpServletRequest request){
        retreatDao.addretreat(id,request);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",arauditorDetailsDao.showDetails(seat,offset,limit));
        map.put("total",arauditorDetailsDao.showDetailsTotal(seat));
        return map;
    }
    //根据销售编号查询信息
    @RequestMapping("/zhans")
    @ResponseBody
    public ArauditorDetails zhans(int id){
        return arauditorDetailsDao.zhans(id);
    }
}
